package com.example.mapper;

import com.example.entity.Cases;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface CasesMapper {

    @Select("<script>" +
            "SELECT * FROM cases " +
            "LIMIT #{offset}, #{limit}" +
            "</script>")
    List<Cases> selectCasesByPage(@Param("offset") int offset, @Param("limit") int limit);

    @Select("SELECT COUNT(*) FROM cases")
    int countCases();


    @Select("<script>" +
            "SELECT * FROM cases WHERE 1=1 " +
            "<if test='titlePart != null and titlePart != \"\"'>AND title LIKE CONCAT('%', #{titlePart}, '%')</if> " +
            "<if test='author != null and author != \"\"'>AND author LIKE CONCAT('%', #{author}, '%')</if> " +
            "LIMIT #{offset}, #{limit}" +
            "</script>")
    List<Cases> selectCasesByTitleOrAuthor(@Param("offset") int offset, @Param("limit") int limit, @Param("titlePart") String titlePart, @Param("author") String author);

    @Select("<script>" +
            "SELECT COUNT(*) FROM cases WHERE 1=1 " +
            "<if test='titlePart != null and titlePart != \"\"'>AND title LIKE CONCAT('%', #{titlePart}, '%')</if> " +
            "<if test='author != null and author != \"\"'>AND author LIKE CONCAT('%', #{author}, '%')</if>" +
            "</script>")
    int countCasesByTitleOrAuthor(@Param("titlePart") String titlePart, @Param("author") String author);

    @Select("SELECT * FROM cases WHERE id = #{id}")
    Cases selectCaseById(@Param("id") Integer id);
}